package metrics_sql

const (
	K8sNodeMemUsageV2 = `
guid = "%v"
hostname = "%v"

from(bucket: "data")
  |> range(start: %v, stop: %v)
  |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
  |> filter(fn: (r) => r["_field"] == "metrics")
  |> filter(fn: (r) => r["name"] == "memory_usage")
  |> filter(fn: (r) => r["agent"] == guid)
  |> filter(fn: (r) => r["instance"] == hostname)
`
	K8sNodeMemUsagelastV2 = `
guid = "%v"
hostname = "%v"

from(bucket: "data")
  |> range(start: %v, stop: %v)
  |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
  |> filter(fn: (r) => r["_field"] == "metrics")
  |> filter(fn: (r) => r["name"] == "memory_usage")
  |> filter(fn: (r) => r["agent"] == guid)
  |> filter(fn: (r) => r["instance"] == hostname)
  |> last()
`
	GluenetsNodeMemUsageV2 = `
guid = "%v"
job1 = guid+"_glue-cadvisor"
job2 = guid+"_glue-node-exporter"

query = (start=%v,stop=%v) => {
    node_memory_free_bytes = from(bucket: "data")
        |> range(start: start, stop: stop)
        |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
        |> filter(fn: (r) => r["_field"] == "node_memory_MemFree_bytes")
        |> filter(fn: (r) => r["job"] == job2)
        |> map(fn: (r) => ({_field:"mem_free_bytes",_value:r._value,_time:r._time,instance:guid}))
        |> window(every: inf)
    node_memory_limit = from(bucket: "data")
        |> range(start: start, stop: stop)
        |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
        |> filter(fn: (r) => r["_field"] == "machine_memory_bytes")
        |> filter(fn: (r) => r["job"] == job1)
        |> last()
        |> map(fn: (r) => ({_field:"node_memory_limit",_value:r._value,_time:r._time,instance:guid}))
        |> window(every: inf)
    return join(tables: {node_memory_free_bytes: node_memory_free_bytes, node_memory_limit: node_memory_limit}, on: ["_start","_stop","instance"])
        |> map(fn: (r) => ({_field:"node_memory_usage",_value: 1.0 - r._value_node_memory_free_bytes /  r._value_node_memory_limit  ,_time:r._time_node_memory_free_bytes,instance:r["instance"],limit: 1}))

}

query()
`
	GluenetsNodeMemUsagelastV2 = `
guid = "%v"
job1 = guid+"_glue-cadvisor"
job2 = guid+"_glue-node-exporter"

query = (start=%v,stop=%v) => {
    node_memory_free_bytes = from(bucket: "data")
        |> range(start: start, stop: stop)
        |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
        |> filter(fn: (r) => r["_field"] == "node_memory_MemFree_bytes")
        |> filter(fn: (r) => r["job"] == job2)
		|> last()
        |> map(fn: (r) => ({_field:"mem_free_bytes",_value:r._value,_time:r._time,instance:guid}))
        |> window(every: inf)
    node_memory_limit = from(bucket: "data")
        |> range(start: start, stop: stop)
        |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
        |> filter(fn: (r) => r["_field"] == "machine_memory_bytes")
        |> filter(fn: (r) => r["job"] == job1)
        |> last()
        |> map(fn: (r) => ({_field:"node_memory_limit",_value:r._value,_time:r._time,instance:guid}))
        |> window(every: inf)
    return join(tables: {node_memory_free_bytes: node_memory_free_bytes, node_memory_limit: node_memory_limit}, on: ["_start","_stop","instance"])
        |> map(fn: (r) => ({_field:"node_memory_usage",_value: 1.0 - r._value_node_memory_free_bytes /  r._value_node_memory_limit  ,_time:r._time_node_memory_free_bytes,instance:r["instance"],limit: 1}))

}

query()
`
)
